package com.pn.service.impl;

import com.pn.dto.AssignRole;
import com.pn.entity.Result;
import com.pn.entity.Role;
import com.pn.entity.User;
import com.pn.mapper.UserMapper;
import com.pn.page.Page;
import com.pn.service.UserService;
import com.pn.utils.DigestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;


    @Override
    public User selectUserByUserCode(String userCode) {
        return userMapper.findUserByUserCode(userCode);
    }

    @Override
    public Page queryUserByPage(User user, Page page) {
        Integer userRowCount = userMapper.findUserRowCount(user);
        List<User> userByPage = userMapper.findUserByPage(user, page);
        page.setTotalNum(userRowCount);
        page.setResultList(userByPage);
        return page;
    }

    @Override
    public Result saveUser(User user) {
        //判断用户是否已存在
        User userinfo = userMapper.findUserByUserCode(user.getUserCode());
        if (userinfo != null){
            return Result.err(601,"用户已存在！");
        }
        //对密码进行加密
        String pwd = DigestUtil.hmacSign(user.getUserPwd());
        user.setUserPwd(pwd);
        int i = userMapper.insertUser(user);
        if (i != 1){
            return Result.err(602,"用户添加失败！");
        }else {
            return Result.ok("用户添加成功！");
        }
    }
    //启动或禁用用户
    @Override
    public Result setUserState(User user) {
        int i = userMapper.updateUserState(user);
        if (i == 1){
            return Result.ok();
        }else {
            return Result.err(603,"启动或禁用用户失败");
        }
    }

    @Override
    public List<Role> findRoleByUserId(Integer userId) {
        return userMapper.selectUserRoleByUserId(userId);
    }

    @Transactional
    @Override
    public int modifyUserRole(AssignRole assignRole) {
        //1、删除userid等于传入userid的数据行
            userMapper.deleteByUserId(assignRole.getUserId());
            for (String s : assignRole.getRoleCheckList()) {
                //2、根据rolename查询roleid
                Integer roleIdByRoleName = userMapper.selectRoleIdByRoleName(s);
                if (roleIdByRoleName != null) {
                    //3、插入查询到的roleid和userid
                    int result = userMapper.insertRoleIdRoleName(roleIdByRoleName, assignRole.getUserId());
                    if(result < 1){
                        return 0;
                    }
                } else {
                    return 0;
                }
            }
        return 1;
    }

    @Override
    public int deleteUserInfo(List<Integer> userIdList) {
        int i = userMapper.deleteUserInfo(userIdList);
        if(i >= 1){
            return 1;
        }else {
            return 0;
        }
    }
    //根据userid修改用户昵称
    @Override
    public int modifyUserName(User user) {
        int i = userMapper.updateUserName(user.getUserId(), user.getUserName(), user.getUpdateBy());
        if (i >= 1){
            return 1;
        }
        return 0;
    }

    @Override
    public int resettingPwd(Integer userId) {
        String pwd = DigestUtil.hmacSign("123456");
        int i = userMapper.updatePwd(pwd, userId);
        if (i > 0){
            return 1;
        }
        return 0;
    }


}
